/* Copyright 2011 Thomas R. Mackel (tmackel@gmail.com) */

package com.civtechtracker;

import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import com.civtechtracker.Tech.TechColors;

public class TechListManager {
	private ArrayList<Tech> tl = new ArrayList<Tech>();
	private CivTechTracker context;

	public TechListManager(CivTechTracker ctx) {
		super();
		context = ctx;
	}

	public Tech getTechByName(String name) {
		for (Tech t : tl) {
			if (t.name.equals(name)) {
				return t;
			}
		}
		return null;
	}

	public Tech getTechById(Integer id) {
		for (Tech t : tl) {
			if (t.id.equals(id)) {
				return t;
			}
		}
		return null;
	}

	public Integer getPoints() {
		Integer score = 0;
		for (Tech t : tl) {
			if (t.owned) {
				score += t.basecost;
			}
		}
		return score;
	}
	
	public List<Credit> getCreditProviders(String name){
		List<Credit> returnList = new ArrayList<Credit>();
		for (Tech t : tl){
			if (t.name != name){
				for (Credit c : t.credits){
					if (c.creditTowards == name){
						returnList.add(new Credit(t.name, c.amount));
					}
				}
			}
		}
		return returnList;
	}

	public List<Tech> getAllTechs() {
		return tl;
	}

	public void addToCart(Integer techId){
		if (!tl.get(techId).owned){
			tl.get(techId).inCart = true;
		}
	}
	
	public void removeFromCart(Integer techId){
		tl.get(techId).inCart = false;
	}
	
	public void removeAllFromCart(){
		for (Tech t : tl){
			t.inCart = false;
		}
	}
	
	public void buyTechsInCart(){
		for (Tech t : tl) {
			if (t.inCart){
				this.buyTech(t.id);
			}
		}
	}
	
	public Integer getCartTotal(){
		Integer total = 0;
		for (Tech t : tl) {
			if (t.inCart){
				total += t.displaycost;
			} 
		}
		return total;
	}
	
	public Integer getCartQty(){
		Integer total = 0;
		for (Tech t : tl) {
			if (t.inCart){
				total++;
			}
		}
		return total;
	}
	
	public void buyTech(Integer techId) {
		Tech purchasedTech = tl.get(techId);
		purchasedTech.owned = true;
		purchasedTech.inCart = false;
		if (0 < purchasedTech.credits.size()) {
			for (Credit c : purchasedTech.credits) {
				Tech creditTech = getTechByName(c.creditTowards);
				if (null != creditTech) {
					creditTech.creditsReceived += c.amount;
					if (creditTech.basecost - creditTech.creditsReceived < 0) {
						creditTech.displaycost = 0;
					} else {
						creditTech.displaycost = creditTech.basecost
								- creditTech.creditsReceived;
					}
				}
			}
		}
	}

	public void unbuyTech(Integer techId) {
		Tech purchasedTech = tl.get(techId);
		if (purchasedTech.owned == true) {
			purchasedTech.owned = false;
			for (Credit c : purchasedTech.credits) {
				Tech creditTech = getTechByName(c.creditTowards);
				if (null != creditTech) {
					creditTech.creditsReceived -= c.amount;
					if (creditTech.basecost - creditTech.creditsReceived < 0) {
						creditTech.displaycost = 0;
					} else {
						creditTech.displaycost = creditTech.basecost
								- creditTech.creditsReceived;
					}
				}
			}
			for (Tech t : tl) {
				if (t.prereq == purchasedTech.name) {
					unbuyTech(t.id);
				}
			}
		}
	}

	public void saveStateInfo(SharedPreferences preferences) {
		char[] ownedIds = new char[tl.size()];
		char[] inCartIds = new char[tl.size()];
		for (int i = 0; i < tl.size(); i++) {
			if (tl.get(i).owned)
				ownedIds[i] = 'T';
			else
				ownedIds[i] = 'F';
			if (tl.get(i).inCart)
				inCartIds[i] = 'T';
			else
				inCartIds[i] = 'F';
		}
		SharedPreferences.Editor editor = preferences.edit();
		editor.putString("ownedTechs", String.valueOf(ownedIds));
		editor.putString("inCartIds", String.valueOf(inCartIds));
		editor.commit();
	}

	public void loadStateInfo(SharedPreferences preferences) {
		init();
		try {
			char[] ownedIds = preferences.getString("ownedTechs", "")
					.toCharArray();
			char[] inCartIds = preferences.getString("inCartIds", "").toCharArray();
			for (int i = 0; i < tl.size(); i++) {
				if (ownedIds[i] == 'T') {
					buyTech(i);
				}
				if (inCartIds[i] == 'T') {
					addToCart(i);
				}
			}
		} catch (Exception e) {
		}
	}

	public void init() {
		tl.clear();
		Tech.resetIdCounter();

		ArrayList<Credit> oranges = new ArrayList<Credit>();
		oranges.add(new Credit(context.getString(R.string.name_pottery), 10));
		oranges.add(new Credit(context.getString(R.string.name_clothmaking), 10));
		oranges.add(new Credit(context.getString(R.string.name_metalworking), 10));
		oranges.add(new Credit(context.getString(R.string.name_agriculture), 10));
		oranges.add(new Credit(context.getString(R.string.name_roadbuilding), 10));
		oranges.add(new Credit(context.getString(R.string.name_mining), 10));
		oranges.add(new Credit(context.getString(R.string.name_engineering), 10));
		oranges.add(new Credit(context.getString(R.string.name_democracy), 10));
		oranges.add(new Credit(context.getString(R.string.name_monotheism), 10));

		ArrayList<Credit> greens = new ArrayList<Credit>();
		greens.add(new Credit(context.getString(R.string.name_mathematics), 20));
		greens.add(new Credit(context.getString(R.string.name_astronomy), 20));
		greens.add(new Credit(context.getString(R.string.name_coinage), 20));
		greens.add(new Credit(context.getString(R.string.name_medicine), 20));
		greens.add(new Credit(context.getString(R.string.name_engineering), 20));
		greens.add(new Credit(context.getString(R.string.name_philosophy), 20));
		greens.add(new Credit(context.getString(R.string.name_theology), 20));

		ArrayList<Credit> blues = new ArrayList<Credit>();
		blues.add(new Credit(context.getString(R.string.name_mathematics), 5));
		blues.add(new Credit(context.getString(R.string.name_dramapoetry), 5));
		blues.add(new Credit(context.getString(R.string.name_music), 5));
		blues.add(new Credit(context.getString(R.string.name_literacy), 5));
		blues.add(new Credit(context.getString(R.string.name_architecture), 5));
		blues.add(new Credit(context.getString(R.string.name_mysticism), 5));
		blues.add(new Credit(context.getString(R.string.name_law), 5));
		blues.add(new Credit(context.getString(R.string.name_democracy), 10));
		blues.add(new Credit(context.getString(R.string.name_enlightenment), 10));

		// Orange techs
		tl.add(new Tech(context.getString(R.string.name_pottery), 45, TechColors.ORANGE, oranges, null,
				context.getString(R.string.desc_pottery)));
		tl.add(new Tech(context.getString(R.string.name_clothmaking), 45, TechColors.ORANGE, oranges, null,
				context.getString(R.string.desc_clothmaking)));
		Tech metalworking = new Tech(context.getString(R.string.name_metalworking), 80, TechColors.ORANGE,
				oranges, null, context.getString(R.string.desc_metalworking));
		metalworking.addOrUpdateCredit(context.getString(R.string.name_military), 20);
		tl.add(metalworking);
		tl.add(new Tech(context.getString(R.string.name_agriculture), 110, TechColors.ORANGE, oranges, null,
				context.getString(R.string.desc_agriculture)));
		tl.add(new Tech(context.getString(R.string.name_roadbuilding), 140, TechColors.ORANGE, oranges,
				context.getString(R.string.name_engineering), context.getString(R.string.desc_roadbuilding)));
		tl.add(new Tech(context.getString(R.string.name_mining), 180, TechColors.ORANGE, oranges,
				context.getString(R.string.name_engineering), context.getString(R.string.desc_mining)));
		Tech engineering = new Tech(context.getString(R.string.name_engineering), 140, TechColors.ENGINEERING,
				oranges, null, context.getString(R.string.desc_engineering));
		for (Credit c : greens) {
			engineering.addOrUpdateCredit(c.creditTowards, c.amount);
		}
		tl.add(engineering);

		// Green techs
		tl.add(new Tech(context.getString(R.string.name_medicine), 140, TechColors.GREEN, greens, null,
				context.getString(R.string.desc_medicine)));
		tl.add(new Tech(context.getString(R.string.name_coinage), 110, TechColors.GREEN, greens, null, context
				.getString(R.string.desc_coinage)));
		tl.add(new Tech(context.getString(R.string.name_astronomy), 80, TechColors.GREEN, greens, null,
				context.getString(R.string.desc_astronomy)));
		Tech mathematics = new Tech(context.getString(R.string.name_mathematics), 230, TechColors.MATHEMATICS,
				greens, null, context.getString(R.string.desc_mathematics));
		for (Credit c : blues) {
			mathematics.addOrUpdateCredit(c.creditTowards, c.amount);
		}
		mathematics.removeCredit(context.getString(R.string.name_democracy));
		mathematics.addOrUpdateCredit(context.getString(R.string.name_philosophy), 25);
		mathematics.addOrUpdateCredit(context.getString(R.string.name_theology), 25);
		tl.add(mathematics);

		// Blue techs
		Tech dramaPoetry = new Tech(context.getString(R.string.name_dramapoetry), 60, TechColors.BLUE,
				blues, null, context.getString(R.string.desc_dramapoetry));
		dramaPoetry.addOrUpdateCredit(context.getString(R.string.name_literacy), 20);
		tl.add(dramaPoetry);
		Tech music = new Tech(context.getString(R.string.name_music), 60, TechColors.BLUE, blues, null,
				context.getString(R.string.desc_music));
		music.addOrUpdateCredit(context.getString(R.string.name_mathematics), 20);
		music.addOrUpdateCredit(context.getString(R.string.name_philosophy), 20);
		tl.add(music);
		Tech architecture = new Tech(context.getString(R.string.name_architecture), 120, TechColors.BLUE,
				blues, null, context.getString(R.string.desc_architecture));
		architecture.addOrUpdateCredit(context.getString(R.string.name_law), 15);
		tl.add(architecture);
		Tech mysticism = new Tech(context.getString(R.string.name_mysticism), 50, TechColors.MYSTICISM, blues,
				null, context.getString(R.string.desc_mysticism));
		mysticism.addOrUpdateCredit(context.getString(R.string.name_deism), 15);
		mysticism.addOrUpdateCredit(context.getString(R.string.name_enlightenment), 15);
		mysticism.addOrUpdateCredit(context.getString(R.string.name_monotheism), 15);
		mysticism.addOrUpdateCredit(context.getString(R.string.name_theology), 15);
		tl.add(mysticism);

		// Yellow techs
		Tech deism = new Tech(context.getString(R.string.name_deism), 80, TechColors.YELLOW, null, null,
				context.getString(R.string.desc_deism));
		deism.addOrUpdateCredit(context.getString(R.string.name_enlightenment), 15);
		deism.addOrUpdateCredit(context.getString(R.string.name_monotheism), 15);
		deism.addOrUpdateCredit(context.getString(R.string.name_theology), 15);
		tl.add(deism);
		Tech enlightenment = new Tech(context.getString(R.string.name_enlightenment), 150, TechColors.YELLOW,
				null, null, context.getString(R.string.desc_enlightenment));
		enlightenment.addOrUpdateCredit(context.getString(R.string.name_monotheism), 15);
		enlightenment.addOrUpdateCredit(context.getString(R.string.name_theology), 15);
		tl.add(enlightenment);
		tl.add(new Tech(context.getString(R.string.name_monotheism), 220, TechColors.YELLOW, null,
				context.getString(R.string.name_enlightenment), context.getString(R.string.desc_monotheism)));
		tl.add(new Tech(context.getString(R.string.name_theology), 250, TechColors.YELLOW, null,
				context.getString(R.string.name_enlightenment), context.getString(R.string.desc_theology)));

		// Red techs
		Tech literacy = new Tech(context.getString(R.string.name_literacy), 110, TechColors.LITERACY, blues,
				null, context.getString(R.string.desc_literacy));
		literacy.addOrUpdateCredit(context.getString(R.string.name_law), 25);
		literacy.addOrUpdateCredit(context.getString(R.string.name_democracy), 25);
		literacy.addOrUpdateCredit(context.getString(R.string.name_philosophy), 25);
		tl.add(literacy);
		tl.add(new Tech(context.getString(R.string.name_military), 180, TechColors.RED, null, null, context
				.getString(R.string.desc_military)));
		tl.add(new Tech(context.getString(R.string.name_law), 170, TechColors.RED, null, null, context
				.getString(R.string.desc_law)));
		tl.add(new Tech(context.getString(R.string.name_democracy), 200, TechColors.RED, null, context.getString(R.string.name_law), context
				.getString(R.string.desc_democracy)));
		tl.add(new Tech(context.getString(R.string.name_philosophy), 240, TechColors.RED, null, context.getString(R.string.name_law), context
				.getString(R.string.desc_philosophy)));
	}

}
